package _08_堆;

import _01_第一季._00_公共接口.Heap;

import java.util.Comparator;

/*
    堆的抽象父类
 */
public abstract class AbstractHeap<E> implements Heap<E>{
    protected int size; //元素个数
    protected Comparator<E> comparator; //支持元素比较

    public AbstractHeap(){
        this(null);
    }

    public AbstractHeap(Comparator<E> comparator){
        this.comparator = comparator;

    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    /**
     * 内部比较元素的方法
     * @param e1
     * @param e2
     * @return
     */
    protected int compare(E e1,E e2){
        return comparator != null ? comparator.compare(e1,e2) : ((Comparable<E>)e1).compareTo(e2);
    }
}
